#include <iostream>
using namespace std;

/*
    C++ 申请堆空间
        new   申请堆空间
            类型名 *指针 = new 类型名;
        delete  释放堆空间
            delete 指针
        申请多个堆空间 -> int *p2 = new int[5];
        释放多个堆空间 -> delete []p2;

        int *p = new int(10)   申请一个int类型大小的堆空间，并初始化为10
*/

int main()
{
    // 分配一个int大小的内存
    int *p = new int; // 等价于 int *p = (int *)malloc(sizeof(int));
    *p = 10;
    cout << *p << endl;

    delete p;
    p = nullptr; // 将指针置空

    // 分配五个int大小的内存
    int *p2 = new int[5]; // 等价于 int *p = (int *)malloc(5 * sizeof(int));
    *p2 = 11;
    cout << *p2 << endl;
    delete[] p2;

    int *p3 = new int(10); // 等价于 int *p = (int *)malloc(sizeof(int)); *p = 10;
    cout << *p3 << endl;
    delete p3;

    return 0;
}